// pages/makesure/makesure.js
const md = require('../../utils/md5.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bottontype: "注册",
    sub: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var types = options.type;
    if (types == "forget") {
      this.setData({
        bottontype: "修改密码",
        types: types
      })
    }
    this.setData({
      phone: options.phone
    })
  },

  formSubmit: function (e) {
    var that = this;
    var sub = that.data.sub;
    if (e.detail.value.password == '') {
      wx.showModal({
        title: '',
        content: '密码不能为空！',
      })
      return;
    }
    if (e.detail.value.password2 == '') {
      wx.showModal({
        title: '',
        content: '确认密码不能为空！',
      })
      return;
    }
    if (e.detail.value.password != e.detail.value.password2) {
      wx.showModal({
        title: '',
        content: '两次键入的密码必须相同！',
      })
      return;
    }
    if (that.data.bottontype == "注册") {
      // var user = res.userInfo;
      // 注册
      // 获取时间戳
      var apinameIsMeb = "/BO/BA_Member/Add";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      var data = {};
      data.Tel = that.data.phone;
      data.PhotoUrl = wx.getStorageSync('avatarUrl');
      data.NickName = wx.getStorageSync('nickName');
      data.Status = 'L';
      data.Password = md.hexMD5(e.detail.value.password);
      data.OpenID = (wx.getStorageSync('openId'));
      if (wx.getStorageSync('fromid')) {
        data.FromMebID = wx.getStorageSync('fromid')
      }
      data.MebCode = that.data.phone;
      // data.MebName = user.nickName;
      // var userinfo = data;
      console.log(data);
      // return;
      sub += 1;
      if (sub != 2) {
        return;
      }
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          data: JSON.stringify(data)
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          that.setData({
            sub: 1
          })
          if (res.data.code == 0) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else {
            wx.showModal({
              title: '',
              content: res.data.msg + '注册失败，请重试~',
            })
            return;
          }
        }
      })

    } else {

      // 获取时间戳
      var apinameIsMeb = "/BO/BA_Member/Query";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          where: "Tel =" + that.data.phone
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.code == 0) {
            if (res.data.data.length > 0) {
              // 获取时间戳
              var apinameIsMeb = "/BO/BA_Member/Modify";
              var timestamp = new Date().getTime();
              timestamp = parseInt(timestamp / 1000);
              var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
              var data = {};
              data.MebID = res.data.data[0].MebID;
              data.Password = md.hexMD5(e.detail.value.password);
              var userinfo = data;
              console.log(data);
              // return;
              wx.request({
                //后台接口地址
                url: app.server.url + apinameIsMeb,
                data: {
                  sign: md5IsMeb,
                  appid: app.server.appid,
                  tmsp: timestamp,
                  data: JSON.stringify(data)
                },
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: function (res) {
                  console.log(res);
                  if (res.data.code == 0) {
                    wx.navigateTo({
                      url: '/pages/login/login',
                    })
                  } else {
                    wx.showModal({
                      title: '',
                      content: res.data.msg + '修改失败，请重试~',
                    })
                    return;
                  }
                }
              })
            } else {
              wx.showModal({
                title: '',
                content: "您的手机号暂未注册~",
              })
              wx.redirectTo({
                url: '/pages/regist/regist',
              })
              return;
            }
            // return;
            // wx.navigateTo({
            //   url: '/pages/login/login',
            // })
          } else {
            wx.showModal({
              title: '',
              content: res.data.msg,
            })
            return;
          }
        }
      })
    }
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
  },
  tologin: function () {
    wx.redirectTo({
      url: '/pages/login/login',
    })
  }
})